package sc.love.boot.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sc.love.boot.kafka.KafkaProducer;
import sc.love.boot.pojo.User;
import sc.love.boot.service.UserService;
import sc.love.boot.spark.SparkUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/boot")
public class BootController {


    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private KafkaProducer kafkaProducer;
    @Autowired
    private SparkUtil sparkUtil;
    @Autowired
    @Qualifier("StaticPorxy")
    private UserService userService;


    @RequestMapping("/index")
    public String index(Model model) {
        model.addAttribute("ss", "123");
        System.out.print("xxxx");
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        return "user/login";
    }

    @PostMapping("/login")
    @ResponseBody
    public String login(String usr, String pas, HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
         try {
//             sparkUtil.writeEs();

             User user = userService.checkUser(usr, pas);


             /*kafka */
             kafkaProducer.send(usr + ":" + pas,"gologin");


             ValueOperations valueOperations = redisTemplate.opsForValue();
             String o = (String)valueOperations.get(usr);
             if (pas.equals(o)) {
                 System.out.println("从redis中取到，直接返回");
                 jsonObject.put("info", "redis success");
                 return jsonObject.toJSONString();
             }



             if ( user.getUserNumber().equals(usr)) {
                 if (user.getPassword().equals(pas)) {
                     jsonObject.put("info", "success");
                     ValueOperations valueOperations1 = redisTemplate.opsForValue();
                     valueOperations1.set(usr, pas);
                     System.out.println("放进redis成功");
                     redisTemplate.expire(usr, 1, TimeUnit.HOURS);
                     Long expire = redisTemplate.getExpire(usr, TimeUnit.MINUTES);
                     System.out.println(expire);
                 } else {
                     jsonObject.put("info", "密码错误");
                 }
             } else {
                 jsonObject.put("info", "用户名不存在");
             }
         } catch (Exception e) {
             jsonObject.put("info", "exception");
            e.printStackTrace();
        }
        return jsonObject.toJSONString();
    }
}
